var gridList;
var moduleTree;
var selectedModule = new Object();
$(document).ready(function () {
    loadTree();
});

function loadTree() {
    gridList = new dataGrid("gridList");
    $.post("/wf/module/list", {"moduleState":"1"}, function (data) {
        if (data.OK == "OK") {
            if (data.List.length > 0){
                selectedModule.moduleId = data.List[0].moduleId;
                selectedModule.moduleName = data.List[0].moduleName;
                gridList.load({
                    'moduleId': selectedModule.moduleId,
                    'activeFlag': 1
                });
                $("#title span").html(data.List[0].moduleName);

                var str = "";
                data.List.forEach(function (value) {
                    str += "<li><a onclick=\"selectModule('"+value.moduleId+"','"+value.moduleName+"')\">" +
                        "<i class=\"layui-icon\" style='top: 1px;position: relative;' id=\""+value.moduleId+"\">&#xe620;</i>" +
                        "<cite>"+value.moduleName+"</cite>" +
                        "</a></li>"
                });
                $("#Tree").html(str);
                $("#Tree li i").eq(0).html("&#xe614;");
            }
        } else {
            layer.msg('取得模块数据失败！', {
                icon: 2,
                time: 1000 //2秒关闭（如果不配置，默认是3秒）
            }, function () {})
        }
    });
}

function selectModule(moduleId,moduleName){
    $("#Tree i").html("&#xe620;");
    $("#Tree #"+moduleId).html("&#xe614;");
    $("#title span").html(moduleName);
    selectedModule.moduleId = moduleId;
    selectedModule.moduleName = moduleName;
    gridList.reload({
        'moduleId': moduleId
    });
}
function refreshData() {
    gridList.reload({
        'activeFlag': 1
    });
}
//查询
function seeAbout() {
    gridList.reload({
        'wfFormVO.displayName': $('#displayName').val()
    });
}
function doEdit(id) {

    var url = "base/workFlow/form/formEdit.html?1=1";
    var title = '添加操作';
    if (id) {
        title = '修改操作';
        url += "&type=edit&fid=" + id;
    }else{
        url += "&type=add";
    }
    if (selectedModule)
        url += '&moduleId=' + selectedModule.moduleId + '&moduleName=' + selectedModule.moduleName;
    parent.layer.open({
        type: 2,
        area: ['800px', '350px'],
        fixed: false, //不固定
        maxmin: true,
        content: url,
        end: function () {
            gridList.reload();
        }
    });
}

function wfState(e) {
    var row=e.record;
    var formId = row.formId;
    var wfState="";
    ActHttpRequest("/wf/workflow/list",{'formEditId':formId },function (data) {
        if(data.List.length >0){
            var activeFlag =  data.List[0].activeFlag;
            if(activeFlag == "3"){
                wfState = "已关闭";
            }else if(activeFlag == "2"){
                wfState = "关闭中";
            }else {
                wfState = "已开启";
            }
        }else{
            wfState = "未绑定流程";
        }
    });

    return wfState;
}

function formatOper(e) {
    var row=e.record;
    var name = row.displayName || '';
     var module = row.moduleId || '';
     var versionId = row.versionId || '';
    var formId = row.formId;
    var btn = "<a class='layui-btn layui-btn-normal layui-btn-xs st_back2' hideFocus='' href='javascript:void(0)' lay-event='del' onClick='moveUp(\""+formId+"\")' style='margin-left: 4px'><i class='layui-icon'>&#xe619;</i>上移</a>"+
        "<a class='layui-btn layui-btn-normal layui-btn-xs st_back2' hideFocus='' href='javascript:void(0)' lay-event='del' onClick='moveDown(\""+formId+"\")' style='margin-left: 4px'><i class='layui-icon'>&#xe61a;</i>下移</a>" +
        "<a class='layui-btn layui-btn-xs' hideFocus='' href='javascript:void(0)' onClick='doEdit(\"" + formId + "\")'><i class='layui-icon'>&#xe642;</i>修改</a>" +
        "<a class='layui-btn layui-btn-xs' hideFocus='' href='javascript:void(0)' onClick='doDesign(\"" + module + "\",\"" + formId + "\",\"" + name + "\")'><i class='layui-icon'>&#xe632;</i>设计</a>" +
     "<a class='layui-btn layui-btn-xs' hideFocus='' href='javascript:void(0)' onClick='saveData(\"" + formId + "\")'><i class='layui-icon'>&#xe630;</i>复制表单</a>"+
        "<a class='layui-btn layui-btn-danger layui-btn-xs' hideFocus='' href='javascript:void(0)' onClick='removeData(\"" + formId + "\",\"" + name + "\")'><i class='layui-icon'>&#xe640;</i>删除</span></a>";
    return btn;
}

function dateRenderer(e) {
    return e.value;
}
/*备份当前版本*/
function saveData(id){
    parent.layer.confirm('确定要备份么？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.post("/wf/form/saveFormById", {"id": id}, function (data) {
            parent.layer.close(parent.layer.index);
            if (data.OK == "OK") {
                parent.layer.msg('操作成功！', {
                    icon: 1,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    , shade: 0.3
                }, function () {
                    gridList.reload();
                })
            } else {
                parent.layer.msg('操作失败！', {
                    icon: 1,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    , shade: 0.3
                }, function () {
                    gridList.reload();
                })
            }
        });
    }, function () {
    });
}

function removeData(fid, title) {
    ActHttpRequest("/wf/workflow/list",{'formEditId':fid },function (data) {
        if(data.List != null && data.List.length > 0 && data.List[0].activeFlag != 3){//(data.List[0].activeFlag != 3){ //bugbug 设计表单时，跳过判断流程状态，上线时删除 "(false){//"
            parent.layer.alert('该表单对应的流程为“已关闭”状态时，才能删除！', {icon:2 });
            return false;
        }else{
            parent.layer.confirm('确定要删除' + title + '么？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                ActHttpRequest("/wf/form/update", {"formId": fid, "activeFlag":"2"}, function (data) {
                    parent.layer.close(parent.layer.index);
                    if (data.OK == "OK") {
                        parent.layer.msg('操作成功！', {
                            icon: 1,
                            time: 2000, //2秒关闭（如果不配置，默认是3秒）
                            shade: [0.3, '#000'],
                            shadeClose: true
                        }, function () {
                            gridList.reload();
                        })
                    } else {
                        parent.layer.msg('操作失败！', {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            gridList.reload();
                        })
                    }
                });
            }, function () {
            });
        }
    })
}

function doDesign(module, formId,title){

    ActHttpRequest("/wf/workflow/list",{'formEditId':formId },function (data) {
        var url = "../froala/froala.html";
        url += "?formId=" + formId + "&moduleId=" +module;
        title = '【' + title + '】' + '表单设计';

        if(data.List.length > 0){
            if(false){//(data.List[0].activeFlag != 3){ //bugbug 设计表单时，跳过判断流程状态，上线时删除 "(false){//"
                parent.layer.alert('该表单对应的流程为“已关闭”状态时，才能进行修改！', {icon:2 });
                return false;
            }else{
                layer.open({
                    type: 2,
                    title: title,
                    content: url,
                    area: ['100%', '100%'],
                    success: function (layerObj) {

                    }
                });
            }
        }else{
            layer.open({
                type: 2,
                title: title,
                content: url,
                area: ['100%', '100%'],
                success: function (layerObj) {

                }
            });
        }
    });

}

function showModuleForms(sender,node,isLeaf){
    var row = sender.row;
    selectedModule.moduleName = row.moduleName;
    refreshData();
}

//上移
function moveUp(e) {
    ActHttpRequest("/wf/form/moveUp",{"id":e},function (data) {
        if(data.OK == "OK"){
            layer.msg(data.msg, {icon: 1, time:1000, shade: 0.3});
        }else {
            layer.msg("操作出错", {icon: 2, time:1000, shade: 0.3});
        }
        gridList.reload();
    });
}

//下移
function moveDown(e) {
    ActHttpRequest("/wf/form/moveDown",{"id":e},function (data) {
        if(data.OK == "OK"){
            layer.msg(data.msg, {icon: 1, time:1000, shade: 0.3});
        }else {
            layer.msg("操作出错", {icon: 2, time:1000, shade: 0.3});
        }
        gridList.reload();
    });
}


//回收站
function recycleBin() {
    layer.open({
        type: 2,
        title: '表单回收站',
        area: ['800px', '610px'],
        fixed: false, //不固定
        maxmin: true,
        content: "../form/formRecycle.html",
        end: function () {
            gridList.reload();
        }
    });
}